package com.ygxsk.carhome.ui.leads;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.blankj.utilcode.util.ActivityUtils;
import com.blankj.utilcode.util.AppUtils;
import com.blankj.utilcode.util.ClickUtils;
import com.blankj.utilcode.util.KeyboardUtils;
import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.NetworkUtils;
import com.blankj.utilcode.util.StringUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemChildClickListener;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.flyco.roundview.RoundTextView;
import com.gyf.immersionbar.ImmersionBar;
import com.scwang.smart.refresh.layout.SmartRefreshLayout;
import com.scwang.smart.refresh.layout.api.RefreshLayout;
import com.scwang.smart.refresh.layout.listener.OnRefreshLoadMoreListener;
import com.tencent.mmkv.MMKV;
import com.trello.rxlifecycle4.android.ActivityEvent;
import com.ygxsk.carhome.R;
import com.ygxsk.carhome.constant.ConstantApi;
import com.ygxsk.carhome.constant.ConstantSign;
import com.ygxsk.carhome.constant.ExtraKey;
import com.ygxsk.carhome.constant.ExtraValue;
import com.ygxsk.carhome.enumwrap.CallHistoryBusinessType;
import com.ygxsk.carhome.enumwrap.LeadCarFromWhere;
import com.ygxsk.carhome.enumwrap.LeadCarStepStatus;
import com.ygxsk.carhome.enumwrap.LeadCarType;
import com.ygxsk.carhome.event.EventLeadMain;
import com.ygxsk.carhome.response.LeadCarTypeBean;
import com.ygxsk.carhome.response.LeadFromWhereBean;
import com.ygxsk.carhome.response.LeadListItemBean;
import com.ygxsk.carhome.response.LeadListItemBeanOutSide;
import com.ygxsk.carhome.retrofit.AddLogUpLoadInfo;
import com.ygxsk.carhome.retrofit.ApiException;
import com.ygxsk.carhome.retrofit.ApiRequestSubListener;
import com.ygxsk.carhome.retrofit.ApiService;
import com.ygxsk.carhome.retrofit.ErrorConsumer;
import com.ygxsk.carhome.retrofit.NetworkManager;
import com.ygxsk.carhome.retrofit.ResponseTransformer;
import com.ygxsk.carhome.sys.Session;
import com.ygxsk.carhome.ui.ErrorActivity;
import com.ygxsk.carhome.ui.adapter.LeadListItemAdapter;
import com.ygxsk.carhome.utils.CommUtils;
import com.ygxsk.carhome.utils.ViewUitls;
import com.ygxsk.carhome.views.ClearEditTextNoClearSK;
import com.ygxsk.carhome.views.MyLinearLayoutManager;
import com.ygxsk.carhome.views.picker.BankPickerDialogFragment;
import com.ygxsk.carhome.views.picker.FromWhereDialogFragment;
import com.ygxsk.carhome.views.picker.NomalCustomBean;
import com.ygxsk.carhome.views.picker.NomalPickerDialogFragment;
import com.ygxsk.carhome.views.spinner.NiceSpinnersk;
import com.ygxsk.integrated.retrofit.Parameterkey;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;

import butterknife.BindView;
import butterknife.internal.DebouncingOnClickListener;
import io.reactivex.rxjava3.disposables.Disposable;
import io.reactivex.rxjava3.functions.Consumer;
import okhttp3.MediaType;
import okhttp3.RequestBody;

/*
 * @Author:itchenqi175@163.com
 * @Time:2023-07-11  15:47
 * @Description:潜在客户主列表内容 使用tabs切换过来
 */
public class LeadMainListActivity extends ErrorActivity {

    //搜索框逻辑
    @BindView(R.id.et_searchincustomsmall)
    ClearEditTextNoClearSK et_searchInCustomSmall;

    //搜索文字 按钮内容
    @BindView(R.id.textviewsearchincustomsmall)
    TextView textViewSearchInCustomSmall;

    @BindView(R.id.refreshlayoutinleadslistactiviy)
    SmartRefreshLayout refreshLayoutInLeadsActivity;

    @BindView(R.id.recyclerviewinleadlistactivity)
    RecyclerView recyclerViewInLeadListActivity;

    //选择类型
    @BindView(R.id.nice_spinner_cartype)
    NiceSpinnersk nice_spinner_carType;
    //选择来源
    @BindView(R.id.nice_spinner_formwhere)
    NiceSpinnersk nice_spinner_formWhere;

    //选择状态
    @BindView(R.id.nice_spinner_stateitem)
    NiceSpinnersk nice_spinner_stateItem;

    //添加潜客
    @BindView(R.id.roundbuttonaddleads)
    RoundTextView roundButtonAddLeads;


    //潜在客户，
    private ArrayList<LeadListItemBean> mDataResources = new ArrayList<>();

    //筛选条件的 汽车的类型；
    private ArrayList<LeadCarTypeBean> dataListCarType = new ArrayList<>();
    //客户的来源
    private ArrayList<LeadFromWhereBean> dataListFromWhere = new ArrayList<>();
    //该客户的分配状态
    private ArrayList<NomalCustomBean> dataListStateItem = new ArrayList<>();

    //适配器
    private LeadListItemAdapter leadListItemAdapter;

    /**
     * 最后一条上传的id的信息内容
     */
    private int pagination_upLoad = Parameterkey.pageNum_default_first;

    //潜客类型 1新车;2二手车
    private String latentType_upLoad = "";
    //客户来源：0预约试驾；1预约看车；2自然到店；3以旧换新
    private String createsource_upLoad = "";
    //跟进状态：0:待联系;1:已分配;2:有意向;3无意向;4成功;5战败;6取消预约
    private String status_upLoad = "";


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        mActivityInstance = this;

        mSession = Session.get(this);

        if (dataListCarType == null) {
            dataListCarType = new ArrayList<>();
        }

        if (dataListFromWhere == null) {
            dataListFromWhere = new ArrayList<>();
        }

        if (dataListStateItem == null) {
            dataListStateItem = new ArrayList<>();
        }

        initloading();

        initViews();

        //默认是直接调用接口的
        gainDatas();

        //其他的类似 监听器的，提交一个客户
        //监听单个客户内容
        //列表之内需要向外部广播数据
        if (!EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().register(this);
        }

    }

    @Override
    protected void initImmersionBar() {
        super.initImmersionBar();

        ImmersionBar.with(this)//
                .titleBar(R.id.toolbarygxskout)
                .fitsSystemWindows(true)//解决状态栏和布局重叠问题，任选其一，默认为false，当为true时一定要指定statusBarColor()，不然状态栏为透明色，还有一些重载方法
                //.statusBarColor(R.color.colorPrimary)//
                //.navigationBarColor(R.color.colorPrimary)//
                //.keyboardEnable(true)
                .init();

    }

    @Override
    protected int getContentViewId() {
        //return R.layout.activity_lead_main_list;
        return R.layout.activity_leadmainlist_activity;
    }

    @Override
    protected void initToolBar(ToolbarHelper toolbarHelper) {

        toolbarHelper.setTitle(getString(R.string.leadscustomer));
        toolbarHelper.setVersionName(true, AppUtils.getAppVersionName());
        toolbarHelper.setLayoutClose(false, "", null);
        toolbarHelper.setLayoutLeft(true, R.drawable.backicon, new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                //搜索客户信息，优先关闭软键盘
                KeyboardUtils.hideSoftInput(mActivityInstance);

                methodBack();

            }
        });

        //添加潜在客户的按钮
        toolbarHelper.setLayoutRight(true, 1, getString(R.string.title_right_addhistory), new ClickUtils.OnDebouncingClickListener() {
            @Override
            public void onDebouncingClick(View v) {

//                //快速双加的限制，
//                Bundle extra_bundle = new Bundle();
//                //标志【添加】
//                extra_bundle.putInt(ExtraKey.EXTRA_JUMPFROM_WHERE, ExtraValue.VALUE_JUMP_FORM_LEAD_ADD);
//                ActivityUtils.startActivity(extra_bundle, LeadAddSubmitActivity.class);

                // TODO: 2024/2/20 12:03 单独一个列表
                Bundle extra_bundle = new Bundle();
//                //标志【添加】
//                extra_bundle.putInt(ExtraKey.EXTRA_JUMPFROM_WHERE, ExtraValue.VALUE_JUMP_FORM_LEAD_ADD);
                ActivityUtils.startActivity(extra_bundle, LeadHistoryActivity.class);


            }
        });

    }

    @Override
    protected void onClickNodata(View v) {

        gainDatas();

    }

    @Override
    protected void initViews() {

        //填充默认的搜索框hint
        if (et_searchInCustomSmall != null) {
            et_searchInCustomSmall.setHint(getString(R.string.search_keyword_full_name));
            et_searchInCustomSmall.clearFocus();
        }

        //使用rxbinding做监听，如果1秒钟没有输入的话就自动清除焦点
//            RxTextView.afterTextChangeEvents(et_searchInCustom)
//                    .debounce(1000, TimeUnit.MILLISECONDS)//
//                    .skip(1)//过滤到第一个空字符的内容
//                    .subscribeOn(AndroidSchedulers.mainThread())//
//                    .observeOn(AndroidSchedulers.mainThread())//
//                    .subscribe(new Consumer<TextViewAfterTextChangeEvent>() {
//                        @Override
//                        public void accept(TextViewAfterTextChangeEvent textViewAfterTextChangeEvent) throws Throwable {
//
//                            //LogUtils.d(ConstantApi.LOGCAT_NOMAL + "监听到的内容是->" + textViewAfterTextChangeEvent.getView().getText().toString());
////                            if (et_searchInCustom.isFocused()) {
////                                //LogUtils.d(ConstantApi.LOGCAT_NOMAL + "afterTextChangeEvents--处于获取焦点状态");
////                                et_searchInCustom.clearFocus();//如果处于占用焦点之中，那么清理焦点
////                            }
//
//                            if (!TextUtils.isEmpty(textViewAfterTextChangeEvent.getView().getText().toString())){
//                                method_SearchSomeThing();
//                            }
//
//                        }
//                    }, new Consumer<Throwable>() {
//
//                        @Override
//                        public void accept(Throwable throwable) throws Throwable {
//                            //
//                        }
//                    });

        //软键盘的【搜索】
        et_searchInCustomSmall.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView textView, int actionId, KeyEvent keyEvent) {

                if (actionId == EditorInfo.IME_ACTION_SEARCH || (keyEvent != null && keyEvent.getKeyCode() == KeyEvent.KEYCODE_ENTER)) {

                    //关闭软键盘
                    KeyboardUtils.hideSoftInput(LeadMainListActivity.this);

                    method_SearchSomeThing();

                    return true;
                }

                return false;
            }
        });

        if (textViewSearchInCustomSmall != null) {

            textViewSearchInCustomSmall.setOnClickListener(new DebouncingOnClickListener() {
                @Override
                public void doClick(View v) {

                    if (KeyboardUtils.isSoftInputVisible(LeadMainListActivity.this)) {
                        KeyboardUtils.hideSoftInput(LeadMainListActivity.this);
                    }

                    method_SearchSomeThing();

                }
            });
        }


        //----------------------------------------------------------------------------------
        //车辆的类型 新车 或者 二手车   潜客类型：1新车;2二手车
        //2024-1-19 09:02:20新增【全部】选项；
        LeadCarTypeBean leadCarTypeBeanNewCardAll = new LeadCarTypeBean();
        leadCarTypeBeanNewCardAll.setDictCode(LeadCarType.LEADTYPE_CAR_ALL.getIndex());
        leadCarTypeBeanNewCardAll.setDictLabel(LeadCarType.LEADTYPE_CAR_ALL.getValue());

        LeadCarTypeBean leadCarTypeBeanNewCard = new LeadCarTypeBean();
        leadCarTypeBeanNewCard.setDictCode(LeadCarType.LEADTYPE_CAR_NEW.getIndex());
        leadCarTypeBeanNewCard.setDictLabel(LeadCarType.LEADTYPE_CAR_NEW.getValue());

        LeadCarTypeBean leadCarTypeBeanOldCar = new LeadCarTypeBean();
        leadCarTypeBeanOldCar.setDictCode(LeadCarType.LEADTYPE_CAR_OLD.getIndex());
        leadCarTypeBeanOldCar.setDictLabel(LeadCarType.LEADTYPE_CAR_OLD.getValue());

        dataListCarType.add(leadCarTypeBeanNewCardAll);
        dataListCarType.add(leadCarTypeBeanNewCard);
        dataListCarType.add(leadCarTypeBeanOldCar);

        //直接存储mmkv的信息
        MMKV.defaultMMKV().encode(ConstantSign.MMKV_JSON_CAR_TYPES_JSON, JSON.toJSONString(dataListCarType));
        //初始化 选择类型
        method_spinnser_carType();
        //----------------------------------------------------------------------------------
        //客户来源：0预约试驾；1预约看车；2自然到店；3以旧换新
//        LEAD_FROM_BOOK_TEST_DRIVE(0,"预约试驾"),
//                LEAD_FROM_BOOK_LOOK_CAR(1,"预约看车"),
//                LEAD_FROM_NATURAL_TO_STORE(2,"自然到店"),
//                LEAD_FROM_OLD_TO_NEW(3,"以旧换新");

        //2024年1月19日09:36:25添加一个【全部】按钮
        LeadFromWhereBean leadFromWhereBeanAll = new LeadFromWhereBean();
        leadFromWhereBeanAll.setDictCode(LeadCarFromWhere.LEAD_FROM_BOOK_TYPE_ALL.getIndex());
        leadFromWhereBeanAll.setDictLabel(LeadCarFromWhere.LEAD_FROM_BOOK_TYPE_ALL.getValue());

        //预约试驾
        LeadFromWhereBean leadFromWhereBean = new LeadFromWhereBean();
        leadFromWhereBean.setDictCode(LeadCarFromWhere.LEAD_FROM_BOOK_TEST_DRIVE.getIndex());
        leadFromWhereBean.setDictLabel(LeadCarFromWhere.LEAD_FROM_BOOK_TEST_DRIVE.getValue());

        //预约看车
        LeadFromWhereBean leadFromWhereBean1 = new LeadFromWhereBean();
        leadFromWhereBean1.setDictCode(LeadCarFromWhere.LEAD_FROM_BOOK_LOOK_CAR.getIndex());
        leadFromWhereBean1.setDictLabel(LeadCarFromWhere.LEAD_FROM_BOOK_LOOK_CAR.getValue());

        //自然到店
        LeadFromWhereBean leadFromWhereBean2 = new LeadFromWhereBean();
        leadFromWhereBean2.setDictCode(LeadCarFromWhere.LEAD_FROM_NATURAL_TO_STORE.getIndex());
        leadFromWhereBean2.setDictLabel(LeadCarFromWhere.LEAD_FROM_NATURAL_TO_STORE.getValue());

        //以旧换新
        LeadFromWhereBean leadFromWhereBean3 = new LeadFromWhereBean();
        leadFromWhereBean3.setDictCode(LeadCarFromWhere.LEAD_FROM_OLD_TO_NEW.getIndex());
        leadFromWhereBean3.setDictLabel(LeadCarFromWhere.LEAD_FROM_OLD_TO_NEW.getValue());

        dataListFromWhere.add(leadFromWhereBeanAll);
        dataListFromWhere.add(leadFromWhereBean);
        dataListFromWhere.add(leadFromWhereBean1);
        dataListFromWhere.add(leadFromWhereBean2);
        dataListFromWhere.add(leadFromWhereBean3);

        //直接存储mmkv的信息
        MMKV.defaultMMKV().encode(ConstantSign.MMKV_JSON_FROM_WHERE_JSON, JSON.toJSONString(dataListFromWhere));
        method_spinnser_formWhere();

        //----------------------------------------------------------------------------------

        //等到真实触发的时候才开始存储，这样，一个页面有多个筛选条件不会造成【后存储的，覆盖掉先存储的】
        //跟进状态：0:待联系;1:初步接洽;2:有意向;3无意向;4成功;5战败;6取消预约
        //LeadCarStepStatus

        //2024年1月19日10:21:59新增分配状态的【全部】
        NomalCustomBean nomalCustomBeanAll = new NomalCustomBean();
        nomalCustomBeanAll.setDictCode(LeadCarStepStatus.LEAD_STEP_ALL.getIndex());
        nomalCustomBeanAll.setDictLabel(LeadCarStepStatus.LEAD_STEP_ALL.getValue());

        //0待联系
        NomalCustomBean nomalCustomBean = new NomalCustomBean();
        nomalCustomBean.setDictCode(LeadCarStepStatus.LEAD_STEP_TO_BE_CONTACT.getIndex());
        nomalCustomBean.setDictLabel(LeadCarStepStatus.LEAD_STEP_TO_BE_CONTACT.getValue());

        //1初步接洽
        NomalCustomBean nomalCustomBean1 = new NomalCustomBean();
        nomalCustomBean1.setDictCode(LeadCarStepStatus.LEAD_STEP_TO_INIT_CONTACT.getIndex());
        nomalCustomBean1.setDictLabel(LeadCarStepStatus.LEAD_STEP_TO_INIT_CONTACT.getValue());

        //2有意向
        NomalCustomBean nomalCustomBean2 = new NomalCustomBean();
        nomalCustomBean2.setDictCode(LeadCarStepStatus.LEAD_STEP_HAS_A_INTENT.getIndex());
        nomalCustomBean2.setDictLabel(LeadCarStepStatus.LEAD_STEP_HAS_A_INTENT.getValue());

        //3无意向
        NomalCustomBean nomalCustomBean3 = new NomalCustomBean();
        nomalCustomBean3.setDictCode(LeadCarStepStatus.LEAD_STEP_HAS_NO_INTENT.getIndex());
        nomalCustomBean3.setDictLabel(LeadCarStepStatus.LEAD_STEP_HAS_NO_INTENT.getValue());

        //4成功  2024-2-21 08:24:03【成功】的状态不能在这里，因为成功状态是一个单独列表
//        NomalCustomBean nomalCustomBean4 = new NomalCustomBean();
//        nomalCustomBean4.setDictCode(LeadCarStepStatus.LEAD_STEP_SUCCESS.getIndex());
//        nomalCustomBean4.setDictLabel(LeadCarStepStatus.LEAD_STEP_SUCCESS.getValue());

        //5战败
        NomalCustomBean nomalCustomBean5 = new NomalCustomBean();
        nomalCustomBean5.setDictCode(LeadCarStepStatus.LEAD_STEP_FAIL.getIndex());
        nomalCustomBean5.setDictLabel(LeadCarStepStatus.LEAD_STEP_FAIL.getValue());

        //6取消预约
        NomalCustomBean nomalCustomBean6 = new NomalCustomBean();
        nomalCustomBean6.setDictCode(LeadCarStepStatus.LEAD_STEP_CANCEL_BOOKING.getIndex());
        nomalCustomBean6.setDictLabel(LeadCarStepStatus.LEAD_STEP_CANCEL_BOOKING.getValue());

        dataListStateItem.add(nomalCustomBeanAll);
        dataListStateItem.add(nomalCustomBean);
        dataListStateItem.add(nomalCustomBean1);
        dataListStateItem.add(nomalCustomBean2);
        dataListStateItem.add(nomalCustomBean3);
//        dataListStateItem.add(nomalCustomBean4);
        dataListStateItem.add(nomalCustomBean5);
        dataListStateItem.add(nomalCustomBean6);

        //直接存储mmkv的信息
        MMKV.defaultMMKV().encode(ConstantSign.MMKV_JSON_NOMAL_CUSTOM_JSON_LEAD_STEP, JSON.toJSONString(dataListStateItem));
        //这个特殊
        method_spinnser_stateItem();
        //----------------------------------------------------------------------------------
        //----------------------------------------------------------------------------------
        //----------------------------------------------------------------------------------
        //布局的适配器填充，下拉刷新等等；
        recyclerViewInLeadListActivity.setLayoutManager(new MyLinearLayoutManager(this));

        leadListItemAdapter = new LeadListItemAdapter(R.layout.leadlistfragmentadapter, mDataResources);
        recyclerViewInLeadListActivity.setAdapter(leadListItemAdapter);

        leadListItemAdapter.notifyDataSetChanged();

        //2024年1月26日15:24:39 因为水波纹效果 所以去掉这个点击
        leadListItemAdapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {

                LeadListItemBean leadListItemBean = (LeadListItemBean) adapter.getItem(position);

                if (leadListItemBean != null) {
                    //携带这条数据，主要是携带id
                    Bundle extra_bundle = new Bundle();
                    extra_bundle.putParcelable(ExtraKey.EXTRA_LEAD_LIST_ITEM_BEAN, leadListItemBean);
                    ActivityUtils.startActivity(extra_bundle, LeadDetailActivity.class);
                }

            }
        });

        leadListItemAdapter.setOnItemChildClickListener(new OnItemChildClickListener() {
            @Override
            public void onItemChildClick(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {

                LeadListItemBean leadListItemBean = (LeadListItemBean) adapter.getItem(position);

                switch (view.getId()) {

//                    case R.id.materialripplelayoutoutsidelead: {//点击外侧的水波纹效果
//
//                        if (leadListItemBean != null) {
////                          //携带这条数据，主要是携带id
//                            Bundle extra_bundle = new Bundle();
//                            extra_bundle.putParcelable(ExtraKey.EXTRA_LEAD_LIST_ITEM_BEAN, leadListItemBean);
//                            ActivityUtils.startActivity(extra_bundle, LeadDetailActivity.class);
//                        }
//
//                        break;
//                    }

                    case R.id.textvieweditviewphone: {

                        clickButton_toCallPhone(LeadMainListActivity.this, leadListItemBean.getCustomerPhone(), CallHistoryBusinessType.BUSINESSTYPE_1.getIndex());

                        break;
                    }

                    default:
                        break;
                }
            }
        });

        refreshLayoutInLeadsActivity.setOnRefreshLoadMoreListener(new OnRefreshLoadMoreListener() {

            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {

                refreshLayout.getLayout().post(new Runnable() {
                    @Override
                    public void run() {

                        pagination_upLoad = Parameterkey.pageNum_default_first;
                        //类型选项重置
                        latentType_upLoad = "";
                        //客户来源选项重置
                        createsource_upLoad = "";
                        //跟进状态
                        status_upLoad = "";

                        //重置3个筛选标题
                        nice_spinner_carType.setText(StringUtils.getString(R.string.please_choose_cartype));
                        nice_spinner_formWhere.setText(StringUtils.getString(R.string.please_choose_formwhere));
                        nice_spinner_stateItem.setText(StringUtils.getString(R.string.please_choose_state_assigned));

                        if (mDataResources != null && !mDataResources.isEmpty()) {
                            mDataResources.clear();
                        }

                        if (leadListItemAdapter != null) {
                            leadListItemAdapter.notifyDataSetChanged();
                        }

                        getServerDatas();

                        refreshLayout.finishRefresh();
                    }
                });

            }

            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {

                refreshLayout.getLayout().post(new Runnable() {
                    @Override
                    public void run() {

                        getServerDatas();

                        refreshLayout.finishLoadMore();

                    }
                });
            }
        });


        if (roundButtonAddLeads != null) {

            roundButtonAddLeads.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                    //快速双加的限制，
                    Bundle extra_bundle = new Bundle();
                    //标志【添加】
                    extra_bundle.putInt(ExtraKey.EXTRA_JUMPFROM_WHERE, ExtraValue.VALUE_JUMP_FORM_LEAD_ADD);
                    ActivityUtils.startActivity(extra_bundle, LeadAddSubmitActivity.class);

                }
            });

        }

    }

    private void method_SearchSomeThing() {

        //获取搜索的关键字内容,刚开始默认是空值
        String keyWord_upLoad = et_searchInCustomSmall.getText().toString().trim();

        if (!TextUtils.isEmpty(keyWord_upLoad) && (keyWord_upLoad).length() < Parameterkey.keyWordLength) {
            //ToastUtils.showShort("最少输入两个字符！");
            CommUtils.checkDialog(mAlertView);
            mAlertView = CommUtils.method_showAlertViewSingleSlowly(LeadMainListActivity.this, ConstantSign.ALERT_TITLE, StringUtils.getString(R.string.pleaseinputkeywordtwo), true);
            mAlertView.show();
            return;
        }

        //如果发起搜索，要重置 第一页
        pagination_upLoad = Parameterkey.pageNum_default_first;
        if (mDataResources != null && !mDataResources.isEmpty()) {
            mDataResources.clear();
        }

        if (leadListItemAdapter != null) {
            leadListItemAdapter.notifyDataSetChanged();
        }

        getServerDatas();

        if (TextUtils.isEmpty(et_searchInCustomSmall.getText().toString().trim())) {
            ToastUtils.make().setGravity(Gravity.CENTER, 0, 0);
            ToastUtils.showShort(getString(R.string.pleaseinputkeyword));
        }

    }

    private void method_spinnser_carType() {

        //默认原始的是【请选择】接口请求完毕，默认填充第一个：目的是：有一个默认的文字的
        nice_spinner_carType.setText(StringUtils.getString(R.string.please_choose_cartype));
        //设置选中文字？
//        nice_spinner_cartype.setTextColor(ColorUtils.getColor(R.color.white));

        //仅仅有点击事件
        nice_spinner_carType.setOnClickListener(new ClickUtils.OnDebouncingClickListener() {
            @Override
            public void onDebouncingClick(View v) {
                //类型是否是动态的？
                if (dataListCarType != null && !dataListCarType.isEmpty()) {

                    //添加类别
                    method_carType_fragmentDialog();

                }
            }
        });


    }

    //客户的来源
    private void method_spinnser_formWhere() {

        //默认原始的是【请选择】接口请求完毕，默认填充第一个：目的是：有一个默认的文字的
        nice_spinner_formWhere.setText(StringUtils.getString(R.string.please_choose_formwhere));
        //设置选中文字？
//        nice_spinner_cartype.setTextColor(ColorUtils.getColor(R.color.white));
        nice_spinner_formWhere.setOnClickListener(new ClickUtils.OnDebouncingClickListener() {
            @Override
            public void onDebouncingClick(View v) {

                //类型是否是动态的？
                if (dataListFromWhere != null && !dataListFromWhere.isEmpty()) {

                    //添加类别
                    method_formWhere_fragmentDialog();

                } else {

                    // : 2023-07-13 17:51 是否从接口获取 ；

                }

            }
        });

    }

    private void method_spinnser_stateItem() {

        //默认原始的是【请选择】接口请求完毕，默认填充第一个：目的是：有一个默认的文字的
        nice_spinner_stateItem.setText(StringUtils.getString(R.string.please_choose_state_assigned));
        //仅仅有点击事件
        nice_spinner_stateItem.setOnClickListener(new ClickUtils.OnDebouncingClickListener() {
            @Override
            public void onDebouncingClick(View v) {

                //类型是否是动态的？
                if (dataListStateItem != null && !dataListStateItem.isEmpty()) {
                    //添加 分配状态
                    method_state_assigned_fragmentDialog();

                } else {
                    // : 2023-07-13 17:51 是否从接口获取 ；
                }
            }
        });
    }

    //添加分配状态的弹窗；
    private void method_state_assigned_fragmentDialog() {

        NomalPickerDialogFragment nomalPickerDialogFragment = NomalPickerDialogFragment.newInstance(getResources().getString(R.string.please_choose_state_assigned), dataListStateItem, ConstantApi.INDEXFLAG_LEADMAINLIST_STATEITEM);
        nomalPickerDialogFragment.setOnNomalChooseListener(new NomalPickerDialogFragment.OnNomalChooseListener() {
            @Override
            public void onNomalChoose(NomalCustomBean nomalCustomBean) {

                if (nomalCustomBean != null) {

                    //2023-03-31 做下拉选择的监听--取出用于上传的 检查方式；
                    status_upLoad = nomalCustomBean.getDictCode();
                    method_clear_datas();
                    //----------------------------------------------------------------------
                    //变动spinner的文本描述；
                    nice_spinner_stateItem.post(new Runnable() {
                        @Override
                        public void run() {
                            nice_spinner_stateItem.setText(nomalCustomBean.getDictLabel());
                        }
                    });

                    //重新保存json
                    MMKV.defaultMMKV().encode(ConstantSign.MMKV_JSON_NOMAL_CUSTOM_JSON_LEAD_STEP, JSON.toJSONString(ViewUitls.method_sort(dataListStateItem, nomalCustomBean)));
                    //-------------------------------------------------------------------------------------------------------------------------------------------------------------------

                } else {

                    //清空重置
                    status_upLoad = "";
                    nice_spinner_stateItem.post(new Runnable() {
                        @Override
                        public void run() {
                            nice_spinner_stateItem.setText(getString(R.string.please_choose_state_assigned));
                        }
                    });

                    method_clear_datas();

                }
            }
        });

        nomalPickerDialogFragment.show(getSupportFragmentManager(), "NomalPickerDialogFragmentItemState");

    }

    //添加弹出是 客户来源的弹窗
    private void method_formWhere_fragmentDialog() {

        FromWhereDialogFragment fromWhereDialogFragment = FromWhereDialogFragment.newInstance(getResources().getString(R.string.please_choose_formwhere), dataListFromWhere);
        fromWhereDialogFragment.setOnFromWhereChooseListener(new FromWhereDialogFragment.OnFromWhereChooseListener() {
            @Override
            public void onFromWhereChoose(LeadFromWhereBean leadFromWhereBean) {

                if (leadFromWhereBean != null) {

                    // : 2023-03-31 客户来源的 筛选
                    createsource_upLoad = leadFromWhereBean.getDictCode();

                    method_clear_datas();

                    //----------------------------------------------------------------------
                    //变动spinner的文本描述；
                    nice_spinner_formWhere.post(new Runnable() {
                        @Override
                        public void run() {
                            nice_spinner_formWhere.setText(leadFromWhereBean.getDictLabel());
                        }
                    });

                    //涉及到变动的其他逻辑；
                    //重新编辑。第一个值内容，为了下次的选中做处理；
                    ArrayList<LeadFromWhereBean> tempAreaCompanyList = new ArrayList<>();
                    if (dataListFromWhere != null && !dataListFromWhere.isEmpty()) {
                        Iterator<LeadFromWhereBean> iterator = dataListFromWhere.iterator();
                        if (iterator != null) {
                            while (iterator.hasNext()) {
                                LeadFromWhereBean item = iterator.next();
                                if (item != null) {
                                    if (TextUtils.equals(leadFromWhereBean.getDictCode(), item.getDictCode())) {
                                        tempAreaCompanyList.add(0, item);//放在第一条
                                    } else {
                                        tempAreaCompanyList.add(item);//顺序存放
                                    }
                                }
                            }
                        }
                    }

                    //重新保存json
                    MMKV.defaultMMKV().encode(ConstantSign.MMKV_JSON_FROM_WHERE_JSON, JSON.toJSONString(tempAreaCompanyList));
                    //--------------------------------------------------------------------------------------------------------------

                } else {

                    //重置清空
                    createsource_upLoad = "";
                    //重新赋值文本
                    if (nice_spinner_formWhere != null) {
                        nice_spinner_formWhere.post(new Runnable() {
                            @Override
                            public void run() {
                                nice_spinner_formWhere.setText(StringUtils.getString(R.string.please_choose_formwhere));
                            }
                        });
                    }

                    method_clear_datas();

                }
            }
        });

        fromWhereDialogFragment.show(getSupportFragmentManager(), "FromWhereDialogFragment");

    }

    //汽车的类型 弹窗；
    private void method_carType_fragmentDialog() {

        //汽车的类型 弹窗；
        BankPickerDialogFragment bankPickerDialogFragment = BankPickerDialogFragment.newInstance(getResources().getString(R.string.please_choose_cartype), dataListCarType);
        bankPickerDialogFragment.setOnCarTypeChooseListener(new BankPickerDialogFragment.OnCarTypeChooseListener() {
            @Override
            public void onCarTypeChoose(LeadCarTypeBean leadCarTypeInfo_choose) {

                LogUtils.json(ConstantSign.LOGCAT_NOMAL + "选中的内容是-->", leadCarTypeInfo_choose);

                //变动都是发生在【该选项不为空的情况下】
                if (leadCarTypeInfo_choose != null) {

                    //变动选择的类型
                    latentType_upLoad = leadCarTypeInfo_choose.getDictCode();

                    method_clear_datas();

                    //选中之后，更改标题；
                    nice_spinner_carType.post(new Runnable() {
                        @Override
                        public void run() {
                            nice_spinner_carType.setText(leadCarTypeInfo_choose.getDictLabel());
                        }
                    });
                    //-----------------------------------------------------------------------------------
                    //重新编辑。第一个值内容，为了下次的选中做处理；
                    ArrayList<LeadCarTypeBean> tempSafetyList = new ArrayList<>();
                    if (dataListCarType != null && !dataListCarType.isEmpty()) {
                        Iterator<LeadCarTypeBean> iterator = dataListCarType.iterator();
                        if (iterator != null) {
                            while (iterator.hasNext()) {
                                LeadCarTypeBean item = iterator.next();
                                if (item != null) {
                                    if (TextUtils.equals(leadCarTypeInfo_choose.getDictCode(), item.getDictCode())) {
                                        tempSafetyList.add(0, item);//放在第一条
                                    } else {
                                        tempSafetyList.add(item);//顺序存放
                                    }
                                }
                            }
                        }
                    }

                    //重新存入json
                    MMKV.defaultMMKV().encode(ConstantSign.MMKV_JSON_CAR_TYPES_JSON, JSON.toJSONString(tempSafetyList));

                } else {
                    //如果为空，那么，可能是点击【确定】为空，可能是选中了:清空重置
                    //条件全部重置
                    //变动选择的类型,【清空重置】就是【全部】；
                    latentType_upLoad = "";

                    nice_spinner_carType.post(new Runnable() {
                        @Override
                        public void run() {
                            nice_spinner_carType.setText(StringUtils.getString(R.string.please_choose_cartype));
                        }
                    });

                    method_clear_datas();

                }
            }
        });

        bankPickerDialogFragment.show(getSupportFragmentManager(), "BankPickerDialogFragmentCarType");

    }

    private void method_clear_datas() {

        pagination_upLoad = Parameterkey.pageNum_default_first;

        //变动之后，要清空数据
        if (mDataResources != null && !mDataResources.isEmpty()) {
            mDataResources.clear();
        }

        if (leadListItemAdapter != null) {
            leadListItemAdapter.notifyDataSetChanged();
        }

        getServerDatas();

    }


    @Override
    protected void gainDatas() {

        //是否有其他需要调用接口的地方；
        //这个接口内就是刷新全部内容
        pagination_upLoad = Parameterkey.pageNum_default_first;//重置

        if (mDataResources != null && !mDataResources.isEmpty()) {
            mDataResources.clear();
        }

        if (leadListItemAdapter != null) {
            leadListItemAdapter.notifyDataSetChanged();
        }

        if (NetworkUtils.isConnected()) {

            getServerDatas();

        } else {
            netWorkError();
            setEnableLoadmore(refreshLayoutInLeadsActivity, false);
        }

    }

    private void getServerDatas() {

        String keyWord_upLoad = et_searchInCustomSmall.getText().toString().trim();

        HashMap<String, Object> map = new HashMap<>();
        map.put(Parameterkey.shopInfoId, mSession.getShopInfoId());//,
        map.put(Parameterkey.latentType, latentType_upLoad);//,潜客类型 1新车;2二手车
        map.put(Parameterkey.createsource, createsource_upLoad);//客户来源：0预约试驾；1预约看车；2自然到店；3以旧换新
        map.put(Parameterkey.status, status_upLoad);////跟进状态：0:待联系;1:已分配;2:有意向;3无意向;4成功;5战败;6取消预约
        map.put(Parameterkey.keyWord, keyWord_upLoad);//,搜索关键字
        map.put(Parameterkey.pagination, pagination_upLoad);//传递的页码
        map.put(Parameterkey.pageSize, Parameterkey.pageSize_default);//,搜索关键字

        AddLogUpLoadInfo addLogUpLoadInfo = new AddLogUpLoadInfo();
        addLogUpLoadInfo.setActivity(mActivityInstance);
        addLogUpLoadInfo.setWhat(ConstantApi.GETSHOPLATENTCUSTOMERLIST_URL);//请求数字标识
        addLogUpLoadInfo.setUrlPath(ApiService.getShopLatentCustomerList_url);//url地址
        addLogUpLoadInfo.setRequestMethod(ConstantSign.REQUEST_METHOD_POST);//POST 或者 GET
        addLogUpLoadInfo.setToastToUser(true);//展示给用户的标志
        addLogUpLoadInfo.setHashMapParameter(map);

        NetworkManager.getInstance().initRetrofitRxJava()
                .create(ApiService.class)
                .getShopLatentCustomerList(RequestBody.create(JSON.toJSONString(map, SerializerFeature.PrettyFormat, SerializerFeature.WriteMapNullValue),
                        MediaType.parse(ApiService.HEADER_JSON)))
                .compose(LeadMainListActivity.this.bindUntilEvent(ActivityEvent.DESTROY))//
                .compose(new ResponseTransformer<>(addLogUpLoadInfo, new ApiRequestSubListener<LeadListItemBeanOutSide>() {
                    @Override
                    public void handlerSomeThingNotSuccessDataForItSelf(int what_method, String serverCode, String serverMessage) {
                        super.handlerSomeThingNotSuccessDataForItSelf(what_method, serverCode, serverMessage);

                        //{"data":"","message":"无数据","code":"20002"}

                        methodNotSuccessData(what_method, serverCode, serverMessage);

                    }
                })).doOnSubscribe(new Consumer<Disposable>() {
                    @Override
                    public void accept(Disposable disposable) throws Throwable {
                        loadingGone();
                        if (refreshLayoutInLeadsActivity != null) {
                            refreshLayoutInLeadsActivity.setEnableLoadMore(true);
                        }
                    }
                }).subscribe(new Consumer<LeadListItemBeanOutSide>() {
                    @Override
                    public void accept(LeadListItemBeanOutSide leadListItemBeanOutSide) throws Throwable {

                        if (leadListItemBeanOutSide != null) {

                            ArrayList<LeadListItemBean> currentDatas = leadListItemBeanOutSide.getDataList();

                            if (currentDatas != null && !currentDatas.isEmpty()) {
                                if (mDataResources != null) {
                                    mDataResources.addAll(currentDatas);
                                }
                            }
                        }

                        //这里也不用管是下拉刷新或者上拉加载更多，直接判断是否为空，
                        if (mDataResources != null && mDataResources.isEmpty()) {
                            //同时禁用加载更多；
                            if (refreshLayoutInLeadsActivity != null) {
                                refreshLayoutInLeadsActivity.setEnableLoadMore(false);
                            }

                            fillNullDataView(StringUtils.getString(R.string.nulldatanow), R.drawable.errorsear);
                        }

                        if (leadListItemAdapter != null) {
                            leadListItemAdapter.notifyDataSetChanged();
                        }

                        //给分页值
                        pagination_upLoad = leadListItemBeanOutSide.getNextPagination();

                        //对列表改造：
                        if (pagination_upLoad == Parameterkey.GET_NEXT_PAGINATION) {
                            setFinishLoadMoreWithNoMoreData(refreshLayoutInLeadsActivity, true);
                        } else {
                            setEnableLoadmore(refreshLayoutInLeadsActivity, true);
                        }

                    }
                }, new ErrorConsumer() {

                    @Override
                    protected void error(ApiException apiException) {

                        //页面报错；

                    }
                });

    }

    @Override
    public void methodNotSuccessData(int what_method, String serverCode, String serverMessage) {
        super.methodNotSuccessData(what_method, serverCode, serverMessage);

        switch (what_method) {

            case ConstantApi.GETSHOPLATENTCUSTOMERLIST_URL: {

                if (mDataResources != null && mDataResources.isEmpty()) {

                    if (refreshLayoutInLeadsActivity != null) {
                        refreshLayoutInLeadsActivity.setEnableLoadMore(false);
                    }

                    CommUtils.checkCurrently((ErrorActivity) mActivityInstance,//
                            serverCode,//
                            R.drawable.errorsear,//
                            serverMessage,//
                            getString(R.string.nulldatanow));
                }

                break;
            }

            default:
                break;
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEventMainThreadInLeadMain(EventLeadMain eventLeadMain) {

        if (eventLeadMain != null) {

            if (eventLeadMain.getMessage() == ExtraValue.EVENTBUS_EVENT_TO_REFRESH_LEADMAINLIST) {

                //如果发起搜索，要重置 第1页
                pagination_upLoad = Parameterkey.pageNum_default_first;

                //清空历史数据
                if (mDataResources != null && !mDataResources.isEmpty()) {
                    mDataResources.clear();
                }

                if (leadListItemAdapter != null) {
                    leadListItemAdapter.notifyDataSetChanged();
                }

                getServerDatas();

            }

        }

    }


    @Override
    protected void onDestroy() {
        super.onDestroy();

        if (mDataResources != null && !mDataResources.isEmpty()) {
            mDataResources.clear();

            if (leadListItemAdapter != null) {
                leadListItemAdapter.notifyDataSetChanged();
            }
            leadListItemAdapter = null;
            mDataResources = null;
        }

        if (dataListCarType != null && !dataListCarType.isEmpty()) {
            dataListCarType.clear();
            dataListCarType = null;
        }

        if (dataListFromWhere != null && !dataListFromWhere.isEmpty()) {
            dataListFromWhere.clear();
            dataListFromWhere = null;
        }

        if (dataListStateItem != null && !dataListStateItem.isEmpty()) {
            dataListStateItem.clear();
            dataListStateItem = null;
        }

        EventBus.getDefault().unregister(this);
    }

    @Override
    protected void methodBack() {
        if (ActivityUtils.isActivityAlive(this)) {
            ActivityUtils.finishActivity(this);
        }
    }
}